import {
  wxRequest
} from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    list: [],
    pageNo: 1,
    hasNextPage: false,
    showModal: false,
    serviceType:1,
    statusList: [ {
      id: 1,
      text: "售后申请"
    }, {
      id: 2,
        text: "处理中"
    }, {
      id: 3,
        text: "申请记录"
    }],
  },

  onLoad:function(options){
    if (options.serviceType){
      this.setData({
        serviceType: options.serviceType
      })
    }
  },

  onShow: function () {
    this.getlist()
  },



  //获取列表
  getlist: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/listService?pageNo=' + this.data.pageNo + '&serviceType=' + this.data.serviceType,
      method: 'POST',
    }, (res) => {

      var list = res.list
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }

      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
      console.log(res, 'res')

    })
  },

  //下拉刷新
  onPullDownRefresh: function () {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function () {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },



  //tabl切换
  hadleTab: function (e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      serviceType: id,
      pageNo:1
    })
    this.getlist()
  },


  //查看详情 
  handleDetail: function (e) {
    wx.navigateTo({
      url: '/pages/good/detail/index?id=' + e.currentTarget.dataset.id
    })
  },


  //退款
  refund: function (e) {
    console.log(e,'eee')
    var showModal = this.data.showModal;
    var odrItemId = e.currentTarget.dataset.itemid;
    var odrId = e.currentTarget.dataset.odrid
    this.setData({
      showModal: !showModal,
      refundReason: '',
      odrItemId,
      odrId
    })
  },



  //监听理由
  onReason: function (e) {
    var inputval = e.detail.value;
    this.setData({
      refundReason: inputval
    })
  },

  //取消
  cancel: function () {
    var showModal = this.data.showModal;
    this.setData({
      showModal: !showModal,
      refundReason: '',
      odrItemId: '',
      odrId:''
    })
  },


  //确定
  confirm: function () {
    if (!this.data.refundReason) {
      wx.showToast({
        icon: 'none',
        title: '退款原因不能为空',
      })
      return
    }
    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定执行此操作",
      success: function (res) {
        if (res.confirm) {
          that.setData({
            showModal: false,
          })
          //参数
          if (that.data.odrItemId) { 
            _wxRequest.wxTonkenAjax({
              url: app.globalData.apiUrl + '/odr/protected/order/refund?odrId=' + that.data.odrId + '&odrItemId=' + that.data.odrItemId + '&refundReason=' + that.data.refundReason,
              method: 'POST',
              // data: params
            }, (res) => {
              that.getlist()
              wx.showToast({
                title: '已提交申请，请耐心等待'
              })
            })
          }
        }
      }
    })
  },



  //申请退款
  refundRequest: function (params) {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/odr/protected/order/refund',
      method: 'POST',
      data: params
    }, (res) => {
      this.getlist()
    })
  },
  
 
})